package com.sidneysimmons.financially.fragments;

import java.io.Serializable;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sidneysimmons.financially.Account;
import com.sidneysimmons.financially.Category;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.adapters.CategoryArrayAdapter;
import com.sidneysimmons.financially.managers.CategoryManager;

public class AccountFragment extends Fragment {
	
	private final String CLASS_NAME = this.getClass().getName();
	
	private Activity context;
	private Account account;
	
	/**
	 * Creates a new AccountFragment instance with the Account set in the 
	 * object, as well as in a bundle for when the fragment is destroyed.
	 * 
	 * @param account - The Account.
	 * @return The new AccountFragment instance.
	 */
	public static AccountFragment newInstance(Account account) {
		Bundle bundle = new Bundle();
		bundle.putSerializable("account", account);
		
		AccountFragment fragment = new AccountFragment();
		fragment.setArguments(bundle);
		fragment.setAccount(account);
		
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.account_page, container, false);
		
		// If the bundle is not null and account is null, restore the account
		// If the bundle is null, the account should already be retained in this object
		if (savedInstanceState != null && account == null) {
			Serializable serialAccount = savedInstanceState.getSerializable("account");
			if (serialAccount != null) {
				account = (Account) serialAccount;
			}
		}

		// Get array of categories
		CategoryManager categoryManager = new CategoryManager(context);
		Category[] categories = categoryManager.getCategoriesArray(account.getTitle());

		// Attach to listview and set click listener
		CategoryArrayAdapter listAdapter = new CategoryArrayAdapter(context, categories);
		ListView listview = (ListView) rootView.findViewById(R.id.account_activity_listview_categories);
		listview.setAdapter(listAdapter);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
				// Get the category that was clicked
				Category category = ((CategoryArrayAdapter) adapterView.getAdapter()).getCategory(position);
				
				// Show the actions menu
				try {
					FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
					
					CategoryActionsFragment categoryActionsFragment = CategoryActionsFragment.newInstance(category);
					categoryActionsFragment.show(fragmentManager, CategoryActionsFragment.TAG);
				} catch (ClassCastException e) {
					Log.e(CLASS_NAME, "Exception thrown while casting 'context' in AccountFragment.java to FragmentActivity.");
					Log.e(CLASS_NAME, "Not showing the Category Actions.");
				}
			}
		});
		
		// Set the account title
		TextView text = (TextView) rootView.findViewById(R.id.account_activity_textview_account_name);
		text.setText(account.getTitle());
		
		// Hide the background help text if there is at least one category
		LinearLayout backgroundInfoLayout = (LinearLayout) rootView.findViewById(R.id.account_activity_layout_background_information);
		if (categories.length > 0) {
			backgroundInfoLayout.setVisibility(View.INVISIBLE);
		} else {
			backgroundInfoLayout.setVisibility(View.VISIBLE);
		}
		
		// Set title bar onclick listener
		LinearLayout title_bar = (LinearLayout) rootView.findViewById(R.id.account_activity_layout_title_bar);
		title_bar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Show Account actions
				try {
					FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
					
					AccountActionsFragment accountActionsFragment = AccountActionsFragment.newInstance();
					accountActionsFragment.show(fragmentManager, AccountActionsFragment.TAG);
				} catch (ClassCastException e) {
					Log.e(CLASS_NAME, "Exception thrown while casting 'context' in AccountFragment.java to FragmentActivity.");
					Log.e(CLASS_NAME, "Not showing the Account Actions.");
				}
			}
		});

		return rootView;
	}
	
	/**
	 * Called when the Fragment and it's Activity are attached.
	 */
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // Set this as the context to use later
        context = activity;
    }
	
	/**
	 * Use this to save any data right before the Fragment is destroyed.
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
		outState.putSerializable("account", account);
	}
	
	/**
	 * Set the Account object.
	 */
	public void setAccount(Account account) {
		this.account = account;
	}
	
	/**
	 * Gets the Account object.
	 * 
	 * @return Account
	 */
	public Account getAccount() {
		return account;
	}
}